﻿using IM.Easy.Core.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IM.Easy.Core.Result
{
    /// <summary>
    /// 通用 API 返回结果基类.
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码，表示操作的结果.
        /// </summary>
        public virtual ResultCode Code { get; set; }

        /// <summary>
        /// 返回的消息，通常用于描述操作的结果或错误信息.
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 标记操作是否成功.
        /// </summary>
        public virtual bool IsSuccess { get; set; }

        /// <summary>
        /// 创建一个表示成功的 API 结果.
        /// </summary>
        /// <param name="msg">返回的消息，默认为 "成功".</param>
        /// <param name="code">状态码，默认为成功状态码 (200).</param>
        /// <returns>表示成功的 API 结果.</returns>
        public static ApiResult Success(string msg = "成功", ResultCode code = ResultCode.Success)
        {
            return new ApiResult
            {
                Code = code,
                Msg = msg,
                IsSuccess = true
            };
        }

        /// <summary>
        /// 创建一个表示失败的 API 结果.
        /// </summary>
        /// <param name="msg">返回的错误消息.</param>
        /// <param name="code">状态码，默认为内部服务器错误 (500).</param>
        /// <returns>表示失败的 API 结果.</returns>
        public static ApiResult Failure(string msg, ResultCode code = ResultCode.InternalServerError)
        {
            return new ApiResult
            {
                Code = code,
                Msg = msg,
                IsSuccess = false
            };
        }
    }


    /// <summary>
    /// 通用 API 返回结果类，包含数据类型的版本.
    /// </summary>
    /// <typeparam name="T">返回的数据类型.</typeparam>
    public class ApiResult<T> : ApiResult
    {
        /// <summary>
        /// 返回的数据.
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 创建一个表示成功的 API 结果.
        /// </summary>
        /// <param name="data">返回的数据.</param>
        /// <param name="msg">返回的消息，默认为 "成功".</param>
        /// <param name="code">状态码，默认为成功状态码 (200).</param>
        /// <returns>表示成功的 API 结果.</returns>
        public static ApiResult<T> Success(T data, string msg = "成功", ResultCode code = ResultCode.Success)
        {
            return new ApiResult<T>
            {
                Code = code,
                Msg = msg,
                Data = data,
                IsSuccess = true
            };
        }

        /// <summary>
        /// 创建一个表示失败的 API 结果.
        /// </summary>
        /// <param name="msg">返回的错误消息.</param>
        /// <param name="code">状态码，默认为内部服务器错误 (500).</param>
        /// <returns>表示失败的 API 结果.</returns>
        public new static ApiResult<T> Failure(string msg, ResultCode code = ResultCode.InternalServerError)
        {
            return new ApiResult<T>
            {
                Code = code,
                Msg = msg,
                Data = default,
                IsSuccess = false
            };
        }
    }

}
